package org.maven.dependencybrowser.controller.model;
/**********************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:

**********************************************************************/
import org.maven.dependencybrowser.model.Artifact;
import org.maven.dependencybrowser.model.ArtifactInstanceMap;
import org.maven.dependencybrowser.model.Duplicate;

import java.util.*;

public class DuplicatesListManager
{
    private Duplicate selected;
    private Map<String, Duplicate> artifactVersions;
    private List<Duplicate> duplicates;
    private List<Duplicate> ordered;
    private ArtifactVersionsListManager artifactVersionsListManager;
    private DuplicatesComparator duplicatesComparator;

    public DuplicatesListManager()
    {
        reset();
    }

    public void reset()
    {
        this.artifactVersionsListManager = new ArtifactVersionsListManager();
        artifactVersions = new HashMap<String, Duplicate>();
        duplicates = new ArrayList<Duplicate>();
        ordered = new ArrayList<Duplicate>();
        duplicatesComparator = new DuplicatesComparator();
    }

    /**
     * Add artifact to existing duplicate or create new duplicate instance
     * @param instance of artifact
     */
    public void add(Artifact instance)
    {
        // check artifact agains duplicate version map
        ArtifactInstanceMap map = artifactVersionsListManager.add(instance);
        String artifactKey = instance.getGroupId() + ":" + instance.getArtifactId();
        Duplicate duplicate = artifactVersions.get(artifactKey);
        if (duplicate == null)
        {
            duplicate = new Duplicate(map, duplicates);
            artifactVersions.put(artifactKey, duplicate);
            ordered.add(duplicate);
            Collections.sort(ordered, duplicatesComparator);
        }
        else
        {
            duplicate.addVersion(map);
        }
    }

    public void setOrderByIdentifier()
    {
        duplicatesComparator.setOrderBy();
        Collections.sort(ordered, duplicatesComparator);
    }

    public ArtifactVersionsListManager getVersionsListManager()
    {
        return artifactVersionsListManager;
    }

    public Duplicate get(int index)
    {
        return ordered.get(index);
    }

    public int size()
    {
        return ordered.size();
    }

    public void select(int index)
    {
        deselect();
        selected = get(index);
        selected.select(true, artifactVersionsListManager);
    }

    public void deselect()
    {
        if (selected != null)
        {
            selected.select(false, artifactVersionsListManager);
        }
    }

    public static class DuplicatesComparator implements Comparator<Duplicate>
    {
        private boolean reversed;

        DuplicatesComparator()
        {
            reversed = false;
        }

        private int implementReversed(int comparison)
        {
            if (reversed)
            {
                return comparison * -1;
            }
            return comparison;
        }

        public void setOrderBy()
        {
            reversed = !reversed;
        }

        public int compare(Duplicate o1, Duplicate o2)
        {
            return implementReversed(o1.getIdentifier().compareToIgnoreCase(o2.getIdentifier()));
        }
    }
}
